<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML>
<html lang="en">
<head>
<base href="<%=basePath%>">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/css/bootstrap.min.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/css/main.css">
<link rel="stylesheet"
	href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css">
<script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/js/main.js"></script>
<script src="${pageContext.request.contextPath}/js/jqueryui.js"></script>
<script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<title>代理商</title>

<script>
	$(function() {
		$("#startDate").datepicker();
		$("#startDate").datepicker("option", "dateFormat", "yy-mm-dd");
	})
</script>
<style>
.main_div {
	min-width: 1000px;
}

.pageChange {
	float: right;
	color: #337ab7;
}

.pageChange>span {
	cursor: pointer
}

.pageChange>span:hover {
	color: blue
}
</style>
</head>
<body>
	<%@ include file="header.jsp"%>
	<%@ include file="left.jsp"%>
	<div id="index"
		style="position: absolute;left: 180px;top: 80px;bottom: 0;right: 0;">
		<section id="main">
			<div class="scrollbar1-1" id="style1-1">
				<div class="main_div" style="height:40px">
					<div class="main_title">
						<span>秒杀管理</span>
					</div>
					<input style="display:none" value="5" id="pageCount"
						name="pageCount" />
					<button style="margin-top:10px" class="btn btn-primary"
						onclick="detailinfos()">添加秒杀广告</button>
				</div>

				<div class="force-overflow1-1">
					<table id="showtable"
						class="table table-condensed table-bordered table-hover"
						style="text-align: center">
						<tr style="background-color: #59c7d2">
							<th style="text-align: center">序号</th>
							<th style="text-align: center">秒杀广告</th>
							<th style="text-align: center">商品名称</th>
							<th style="text-align: center">商品原价</th>
							<th style="text-align: center">秒杀价格</th>
							<th style="text-align: center">商品数量</th>
							<th style="text-align: center">开始日期</th>
							<th style="text-align: center">状态</th>
							<th style="text-align: center">修改</th>
							<th style="text-align: center">删除</th>
						</tr>
					</table>
					<div class="pageChange" style="margin-bottom:30px">
						<span onclick="prePage()">&nbsp;&nbsp;上一页&nbsp;&nbsp;</span> <span
							onclick="nextPage()">&nbsp;&nbsp;下一页&nbsp;&nbsp;</span> <em>
							&nbsp; 第&nbsp;<span id="pageNum">1</span>&nbsp;页&nbsp;&nbsp;
						</em> <em>/&nbsp;&nbsp;共&nbsp;<span id="totalpage"></span>&nbsp;页
						</em> <em>&nbsp;&nbsp;&nbsp;跳到第<input id="searchInput"
							style="width:46px" maxlength="4" type="text">页
						</em>
						<button class="btn btn-primary" onclick="searchPage()">搜索</button>
					</div>
				</div>
			</div>
		</section>
		<script>
			$(function() {
				selectinfo();
			})
			
			function selectinfo() {
				var pageNum = $("#pageNum").html();
				var pageCount = $("#pageCount").val();
		
				$.post(urls + "seckillAction/selectSeckillProductList",
					{
						agentId : localStorage.getItem("agentid"),
						typeId : 0,
						pageNum : pageNum,
						pageCount : pageCount,
					},
					function(data) {
						//console.log(data)
						if (data) {
							var totalpage = data.totalpage;
							var orderlist = data.list;
							var table = $("#showtable");
							$("tr").remove(".addtr");
							for (var i = 0; i < orderlist.length; i++) {
								var status,
									disableds = "";
								if (orderlist[i].status == 0) {
									status = "未审核";
								//disableds=;
								} else if (orderlist[i].status == 1) {
									status = "审核通过";
									disableds = "disabled";
								} else if (orderlist[i].status == 2) {
									status = "未通过";
								//disableds=;
								} else if (orderlist[i].status == 3) {
									status = "已发布";
									disableds = "disabled";
								} else if (orderlist[i].status == 4) {
									status = "已过期";
								//disableds=;
								}
								var append = "<tr id='" + orderlist[i].id + "'  class='addtr'><td style='line-height:120px'>" + (i + 1) + "</td>" +
		
									"<td ><img style='width:250px;height:120px' src='/seckills/" + orderlist[i].publicityImg + "' alt='加载中'/> </td>" +
									"<td style='vertical-align:middle;'>" + orderlist[i].productName + "</td>" +
									"<td style='line-height:120px'>" + orderlist[i].productPrice + "</td>" +
									"<td style='line-height:120px'>" + orderlist[i].seckillPrice + "</td>" +
									"<td style='line-height:120px'>" + orderlist[i].inventory + "</td>" +
									"<td style='line-height:120px'>" + orderlist[i].startDate + "</td>" +
									"<td style='line-height:120px'>" + status + "</td>" +
									"<td style='line-height:120px'><input onclick='detailinfo(\"" +
									orderlist[i].productName + "|$" +
									orderlist[i].productPrice + "|$" +
									orderlist[i].seckillPrice + "|$" +
									orderlist[i].inventory + "|$" +
									orderlist[i].startDate + "|$" +
									orderlist[i].startTime + "|$" +
									orderlist[i].endTime + "|$" +
									orderlist[i].details + "|$" +
									orderlist[i].productImg + "|$" +
									orderlist[i].publicityImg + "|$" +
									orderlist[i].id + "|$" +
									orderlist[i].detailImg +
									"\")' type='button' class='btn btn-primary' value='修改' " +
									disableds + "/></td>" +
									"<td style='line-height:120px'><input onclick='deletBurst(\"" + orderlist[i].id + "\")' type='button' class='btn btn-primary' value='删除' " +
									disableds + "/></td></tr>";
								table.append(append);
							}
							$("#totalpage").html(totalpage);
						}
					}
					, "json"
				)
			}
			function deletBurst(e) { //删除爆款
				if (confirm("是够删除商品？")) {
					$.post(urls + "seckillAction/deleteSeckillOne",
						{
							id : e
						},
						function(res) {
							if (res) {
								$("#" + e).css("display", "none")
							} else {
								alert("删除商品失败")
							}
						})
				}
			}
			function detailinfos() { //添加		
				location.href = "${pageContext.request.contextPath}/YMGback/agent/addSpike.jsp"
			}
			function detailinfo(e) { //点击修改
				$.post(urls + "seckillAction/getOtherInfo", {
					agentId : localStorage.getItem("agentid")
				}, function(res) {
					if (res) {
						$("#selectOther").empty()
						$("#selectOther").append("<option value='-1'>请选择</option>")
						$.each(res, function(n, OtherInfo) {
							$("#selectOther").append("<option value=" + OtherInfo.id + ">" + OtherInfo.otherName + "</option>");
						})
					}
				})
				//console.log(e)
				$("#motai").css("display", "block");
				$("#main").css("display", "none");
				var attr = e.split("|$");
				$("#image").attr("src", "/seckills/" + attr[8]);
				$("#image2").attr("src", "/seckills/" + attr[9]);
				$("#image3").attr("src", "/seckills/" + attr[11]);
				$("#productName").val(attr[0]);
				$("#productPrice").val(attr[1]);
				$("#seckillPrice").val(attr[2]);
				$("#inventory").val(attr[3]);
				$("#startDate").val(attr[4]);
				$("#startTime").val(attr[5]);
				$("#endTime").val(attr[6]);
				$("#details").val(attr[7]);
				$("#id").val(attr[10]);
		
			}
		</script>
		<section id="motai" style="display:none">
			<div class="scrollbar1-1" id="style1-1">
				<div class="main_div" style="height: 560px;">
					<div class="main_title">
						<span>修改商品</span>
					</div>
					<form class="form-horizontal" enctype="multipart/form-data"
						style="margin-top: 30px" id="uploadForm">
						<div class="form-group">
							<label class="col-xs-2 control-label">商家名称：</label>
							<div class="col-xs-3">
								<select id="selectOther" class="form-control" name="otherId">

								</select>
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">商品名称：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="productName"
									name="productName" placeholder="商品名称"> <input name="id"
									type="text" style="display:none" id="id">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">商品原价：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="productPrice"
									name="productPrice" placeholder="请输入商品原价">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">秒杀价格：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="seckillPrice"
									maxlength="6" name="seckillPrice" placeholder="请输入商品秒杀价格">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">商品库存：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="inventory"
									maxlength="6" name="inventory" placeholder="请输入商品库存">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">秒杀日期：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="startDate"
									name="startDate" placeholder="请输入秒杀日期">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">开始时间：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="startTime"
									value="19:00:00" name="startTime" placeholder="请输入活动开始时分秒">
							</div>
							<label class="col-xs-3 control-label">请输入正确时间格式xx:xx:xx，如12:00:09</label>
						</div>

						<div class="form-group">
							<label class="col-xs-2 control-label">结束时间：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="endTime"
									value="23:59:59" name="endTime" placeholder="请输入活动结束时分秒">
							</div>
							<label class="col-xs-3 control-label">请输入正确时间格式xx:xx:xx，如12:00:09</label>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">广告图片：</label>
							<div class="col-xs-3">
								<input type="file" id="publicityFile" name="publicityFile"
									onchange="selectImage2(this);"> <input type="text"
									id="agentId" name="agentId" style="display:none"> <img
									id="image2" style="width:375px;height:150px" src="" /> <input
									type="text" value="0" name="status" style="display:none">
							</div>
							<label class="col-xs-5 control-label"
								style="text-align: left;color: #ff6c29">*必须上传广告图片（建议大小为750*300像素）</label>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">商品图片：</label>
							<div class="col-xs-3">
								<input type="file" id="productFile" name="productFile"
									onchange="selectImage(this);" /> <img id="image"
									style="width:375px;height:375px" src="" />
							</div>
							<label class="col-xs-5 control-label"
								style="text-align: left;color: #ff6c29">*必须上传商品图片（建议大小为750*750像素)</label>
						</div>
						
						<div class="form-group">
							<label class="col-xs-2 control-label">商品详情图片：</label>
							<div class="col-xs-3">
								<input type="file" id="detailFile" name="detailFile"
									onchange="selectImage3(this);"> <img id="image3"
									style="width:375px;" src="" />
							</div>
							<label class="col-xs-5 control-label"
								style="text-align: left;color: #ff6c29">*必须上传商品详情图片（建议宽度大小为750像素）</label>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">商品详情：</label>
							<div class="col-xs-4">
								<textarea class="col-xs-12" rows="5" id="details" name="details"></textarea>
							</div>

						</div>

						<div class="col-xs-4 col-xs-offset-2">
							<button id="setButton" class="col-xs-4 btn btn-primary"
								type="button">提交审核</button>
						</div>

					</form>
				</div>
			</div>
		</section>
	</div>
	<script>
		var image = '';
		function selectImage(file) { //显示图片
			if (!file.files || !file.files[0]) {
				return;
			}
			var reader = new FileReader();
			reader.onload = function(evt) {
				document.getElementById('image').src = evt.target.result;
				image = evt.target.result;
			};
			reader.readAsDataURL(file.files[0]);
		}
		function selectImage2(file) { //显示图片
			if (!file.files || !file.files[0]) {
				return;
			}
			var reader = new FileReader();
			reader.onload = function(evt) {
				document.getElementById('image2').src = evt.target.result;
				image = evt.target.result;
			};
			reader.readAsDataURL(file.files[0]);
		}
		function selectImage3(file) { //显示图片
			if (!file.files || !file.files[0]) {
				return;
			}
			var reader = new FileReader();
			reader.onload = function(evt) {
				document.getElementById('image3').src = evt.target.result;
				image = evt.target.result;
			};
			reader.readAsDataURL(file.files[0]);
		}
		$(function() { //ajax异步提交表单，好东西
	
			$("#agentId").val(localStorage.getItem("agentid"))
			$("#setButton").click(function() {
				var reg = /^(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/;
				var config = true;
				if (reg.test($("#startTime").val()) && reg.test($("#endTime").val())) {
	
				} else {
					alert("请输入正确的时间，如：12:09:00")
					return;
				}
				var reg2 = /(^[1-9]([0-9]{1,3})?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
				if (reg2.test($("#productPrice").val()) && reg2.test($("#seckillPrice").val())) {
	
				} else {
					alert("请输入正确的价格，如：12.22")
					return;
				}
				if ($("#selectOther").find("option:selected").val() == -1) {
					alert("请选择商家名称")
					return;
				}
				/********************用于判断图片大小以及图片名称是否合法******************/
				var aarr = document.getElementsByTagName("input");
				var barr = [];
				var picreg = /^[A-Za-z0-9\._]{1,100}$/; //图片名称正则
				var flag1 = false; //用于判断图片大小
				var flag2 = false; //用于判断图片名称
				for (var i = 0; i < aarr.length; i++) {
					if (aarr[i].type == "file") {
						barr.push(aarr[i])
					}
				}
				for (var z = 0; z < barr.length; z++) {
					if (barr[z].files.length == 0) {
					} else {
						if (barr[z].files[0].size > 204800) { //判断图片大小是否大于200kb
							flag1 = true;
						}
						if (!picreg.test(barr[z].files[0].name)) { //判断图片大小是否大于200kb
	
							flag2 = true;
						}
					}
				}
				if (flag1) {
					alert("上传图片不得超过200kb每张");
					return;
				}
				if (flag2) {
					alert("图片名称只能由字母与数字组成");
					return;
				}
				/******************用于判断图片大小以及图片名称是否合法****************/
				$.ajax({
					url : urls + "seckillAction/updateSeckillOne",
					type : 'POST',
					cache : false,
					data : new FormData($('#uploadForm')[0]),
					processData : false,
					contentType : false
				}).done(function(res) {
					if (res == true) {
						alert("提交成功")
						$("#motai").css("display", "none");
						$("#main").css("display", "block");
						selectinfo()
					} else if (res == 251) {
						alert("该商品已审核");
						$("#motai").css("display", "none");
						$("#main").css("display", "block");
						selectinfo()
					} else if (res == false) {
						alert("提交失败")
					}
				}).fail(function(res) {});
			})
	
		})
	</script>
</body>
</html>
